/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JdhLoginDialog.java
 *
 * Created on 2009-9-9, 16:43:55
 */

package jdh;

import javax.swing.JOptionPane;

/**
 *
 * @author tale
 */
public class JdhLoginDialog extends javax.swing.JDialog {

    /** Creates new form JdhLoginDialog */
    public JdhLoginDialog(java.awt.Frame parent, boolean modal, Backend backend) {
        super(parent, modal);
        initComponents();

        // use initial
        fillSqlTypes();
        serverConfigPanel.setVisible(false);
        serverConfigButton.setSelected(false);
        setSize(getPreferredSize());
        server = backend;
        reload();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        idType = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        typeAdmin = new javax.swing.JRadioButton();
        typeTeacher = new javax.swing.JRadioButton();
        typeStudent = new javax.swing.JRadioButton();
        jLabel6 = new javax.swing.JLabel();
        userid = new javax.swing.JTextField();
        password = new javax.swing.JPasswordField();
        jLabel7 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        quitButton = new javax.swing.JButton();
        loginButton = new javax.swing.JButton();
        serverConfigButton = new javax.swing.JToggleButton();
        serverConfigPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        serverLocation = new javax.swing.JTextField();
        serverType = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        serverDatabase = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        serverUsername = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        serverPassword = new javax.swing.JPasswordField();
        defaultButton = new javax.swing.JButton();
        testButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jdh.JdhApp.class).getContext().getResourceMap(JdhLoginDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        idType.add(typeAdmin);
        typeAdmin.setSelected(true);
        typeAdmin.setText(resourceMap.getString("typeAdmin.text")); // NOI18N
        typeAdmin.setName("typeAdmin"); // NOI18N

        idType.add(typeTeacher);
        typeTeacher.setText(resourceMap.getString("typeTeacher.text")); // NOI18N
        typeTeacher.setName("typeTeacher"); // NOI18N

        idType.add(typeStudent);
        typeStudent.setText(resourceMap.getString("typeStudent.text")); // NOI18N
        typeStudent.setName("typeStudent"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        userid.setText(resourceMap.getString("userid.text")); // NOI18N
        userid.setName("userid"); // NOI18N

        password.setText(resourceMap.getString("password.text")); // NOI18N
        password.setName("password"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(112, 112, 112)
                .addComponent(typeAdmin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(typeTeacher, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(typeStudent, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
                .addGap(120, 120, 120))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(59, 59, 59)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.CENTER, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(password, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.CENTER, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(18, 18, 18)
                        .addComponent(userid, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)))
                .addGap(58, 58, 58))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(typeTeacher)
                    .addComponent(typeStudent)
                    .addComponent(typeAdmin))
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(userid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        jPanel2.setName("jPanel2"); // NOI18N

        quitButton.setText(resourceMap.getString("quitButton.text")); // NOI18N
        quitButton.setName("quitButton"); // NOI18N
        quitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitButtonActionPerformed(evt);
            }
        });

        loginButton.setText(resourceMap.getString("loginButton.text")); // NOI18N
        loginButton.setName("loginButton"); // NOI18N
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });

        serverConfigButton.setText(resourceMap.getString("serverConfigButton.text")); // NOI18N
        serverConfigButton.setName("serverConfigButton"); // NOI18N
        serverConfigButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                serverConfigButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(serverConfigButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 221, Short.MAX_VALUE)
                .addComponent(loginButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(quitButton)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(quitButton)
                    .addComponent(loginButton)
                    .addComponent(serverConfigButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        serverConfigPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("serverConfigPanel.border.title"))); // NOI18N
        serverConfigPanel.setName("serverConfigPanel"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        serverLocation.setText(resourceMap.getString("serverLocation.text")); // NOI18N
        serverLocation.setName("serverLocation"); // NOI18N

        serverType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        serverType.setName("serverType"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        serverDatabase.setText(resourceMap.getString("serverDatabase.text")); // NOI18N
        serverDatabase.setName("serverDatabase"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        serverUsername.setText(resourceMap.getString("serverUsername.text")); // NOI18N
        serverUsername.setName("serverUsername"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        serverPassword.setText(resourceMap.getString("serverPassword.text")); // NOI18N
        serverPassword.setName("serverPassword"); // NOI18N

        defaultButton.setText(resourceMap.getString("defaultButton.text")); // NOI18N
        defaultButton.setName("defaultButton"); // NOI18N
        defaultButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                defaultButtonActionPerformed(evt);
            }
        });

        testButton.setText(resourceMap.getString("testButton.text")); // NOI18N
        testButton.setName("testButton"); // NOI18N
        testButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout serverConfigPanelLayout = new javax.swing.GroupLayout(serverConfigPanel);
        serverConfigPanel.setLayout(serverConfigPanelLayout);
        serverConfigPanelLayout.setHorizontalGroup(
            serverConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(serverConfigPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(serverConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(serverConfigPanelLayout.createSequentialGroup()
                        .addGroup(serverConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(serverConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(serverPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                            .addComponent(serverUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                            .addComponent(serverDatabase, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                            .addComponent(serverLocation, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                            .addComponent(serverType, 0, 346, Short.MAX_VALUE)))
                    .addGroup(serverConfigPanelLayout.createSequentialGroup()
                        .addComponent(defaultButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 251, Short.MAX_VALUE)
                        .addComponent(testButton)))
                .addContainerGap())
        );
        serverConfigPanelLayout.setVerticalGroup(
            serverConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(serverConfigPanelLayout.createSequentialGroup()
                .addGroup(serverConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(serverType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(serverConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(serverLocation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(serverConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(serverDatabase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(serverConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(serverUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(serverConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(serverPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addGroup(serverConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(defaultButton)
                    .addComponent(testButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(serverConfigPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(serverConfigPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginButtonActionPerformed
        this.apply();
        currentUserID = userid.getText();
        if (typeAdmin.isSelected()) currentUserType = 0;
        else if (typeTeacher.isSelected()) currentUserType = 1;
        else currentUserType = 2;
        if (server.checkToLogin(currentUserID, password.getPassword(), currentUserType) == false)
            JOptionPane.showMessageDialog(null, "用户ID或者密码错误。", "失败", JOptionPane.ERROR_MESSAGE);
        else
            this.dispose();
    }//GEN-LAST:event_loginButtonActionPerformed

    private void serverConfigButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverConfigButtonActionPerformed
        serverConfigPanel.setVisible(serverConfigButton.isSelected());
        this.setSize(this.getPreferredSize());
    }//GEN-LAST:event_serverConfigButtonActionPerformed

    private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testButtonActionPerformed
        this.apply();
        if (server.test() == false)
            JOptionPane.showMessageDialog(null, "无法连接至服务器。", "失败", JOptionPane.ERROR_MESSAGE);
        else
            JOptionPane.showMessageDialog(null, "服务器工作良好。", "恭喜", JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_testButtonActionPerformed

    private void defaultButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_defaultButtonActionPerformed
        System.out.println("Reload default database.");
        Backend temp = server;
        setServer(Backend.getDefaultBackend());
        reload();
        setServer(temp);
    }//GEN-LAST:event_defaultButtonActionPerformed

    private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitButtonActionPerformed
        System.exit(0);
    }//GEN-LAST:event_quitButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JdhLoginDialog dialog = new JdhLoginDialog(new javax.swing.JFrame(), true, new Backend());
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton defaultButton;
    private javax.swing.ButtonGroup idType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton loginButton;
    private javax.swing.JPasswordField password;
    private javax.swing.JButton quitButton;
    private javax.swing.JToggleButton serverConfigButton;
    private javax.swing.JPanel serverConfigPanel;
    private javax.swing.JTextField serverDatabase;
    private javax.swing.JTextField serverLocation;
    private javax.swing.JPasswordField serverPassword;
    private javax.swing.JComboBox serverType;
    private javax.swing.JTextField serverUsername;
    private javax.swing.JButton testButton;
    private javax.swing.JRadioButton typeAdmin;
    private javax.swing.JRadioButton typeStudent;
    private javax.swing.JRadioButton typeTeacher;
    private javax.swing.JTextField userid;
    // End of variables declaration//GEN-END:variables


    // user definitions
    private Backend server;
    public int currentUserType;
    public String currentUserID;

    public void setServer(Backend server){
        this.server = server;
    }

    private void reload(){
        serverLocation.setText(server.getServer());
        serverDatabase.setText(server.getDatabase());
        serverUsername.setText(server.getUsername());
        serverPassword.setText(server.getPassword());
        ServerConfig.engineType[] types = ServerConfig.engineType.values();
        for (int i = 0 ; i < types.length; i ++){
            if (server.getType() == types[i])
                serverType.setSelectedIndex(i);
        }
    }

    private void apply(){
        int type = serverType.getSelectedIndex();
        server.setType(ServerConfig.engineType.values()[type]);
        server.setServer(serverLocation.getText());
        server.setDatabase(serverDatabase.getText());
        server.setUsername(serverUsername.getText());
        server.setPassword(String.valueOf(serverPassword.getPassword()));
    }

    private void fillSqlTypes(){
        ServerConfig.engineType[] types = ServerConfig.engineType.values();
        serverType.removeAllItems();
        for (int i = 0 ; i < types.length; i ++){
            serverType.addItem(types[i]);
        }
    }

}
